package com.dts.evaluate.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dts.evaluate.R;
import com.dts.evaluate.bean.ProjectDataBean;
import com.dts.evaluate.bean.Worker;
import com.dts.evaluate.callback.BeanCallBack;
import com.dts.evaluate.common.Constants;
import com.dts.evaluate.controller.impl.DataImpl;
import com.dts.evaluate.controller.intf.DataInterface;
import com.dts.evaluate.ui.view.TitleView;
import com.dts.evaluate.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class KbsxActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TitleView titleView;
    @Bind(R.id.kbsx_listview)
    ListView kbsxListview;

    private TableListAdapter adapter;
    private int type = Constants.TYPE_PARENT;//默认一级请求类型
    private String dataid = "";//ya-ScGsc-Org-2008-11
    private Worker mWorker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kbsx);
        ButterKnife.bind(this);

        //初始化
        initView();


        /**
         * 判断请求类型，如果为一级请求，则approveProjectBean为null，type会默认一级请求类型
         */
        Intent intent = getIntent();
        mWorker = (Worker) intent.getSerializableExtra(Constants.WORKER);
        if (mWorker != null) {
            dataid = mWorker.getGscOrganCodeId();
            //dataid = "ya-ScGsc-Org-2008-11";
        }

        ProjectDataBean.ApproveProjectBean approveProjectBean;
        if (intent != null && (approveProjectBean = (ProjectDataBean.ApproveProjectBean) intent.getSerializableExtra(Constants.APPROVE_PROJECT_BEAN)) != null) {
            type = Constants.TYPE_CHILD;
            dataid = approveProjectBean.getAppprojectcodeid();
        }

        /**
         * 网络请求
         */
        DataInterface dataInterface = new DataImpl();
        dataInterface.getKbsxData(Constants.KBSX_URL, type, dataid, new BeanCallBack<ProjectDataBean>() {

            @Override
            public void onResponse(boolean isFromCache, ProjectDataBean projectDataBean, Request request, @Nullable Response response) {
                if (projectDataBean == null) {
                    return;
                }
                List<ProjectDataBean.ApproveProjectBean> parentBeanList = projectDataBean.getApproveProject();
                List<ProjectDataBean.ApproveProjectBean> childBeanList = projectDataBean.getApproveProjectdetail();
                switch (type) {
                    case Constants.TYPE_PARENT:
                        if (parentBeanList != null) {
                            adapter.setData(parentBeanList);
                        }
                        break;
                    case Constants.TYPE_CHILD:
                        if (childBeanList != null) {
                            adapter.setData(childBeanList);
                        }
                        break;

                }
            }

            @Override
            public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                super.onError(isFromCache, call, response, e);
                if (isFromCache) {
                    ToastUtils.show(KbsxActivity.this, R.string.load_errot);
                }
            }
        }, this);

    }

    private void initView() {
        //为listview添加header
        LayoutInflater inflater = LayoutInflater.from(this);
        inflater = LayoutInflater.from(this);
        View header = inflater.inflate(R.layout.kbsx_table_header_layout, null);
        kbsxListview.addHeaderView(header);

        //设置listview适配器
        adapter = new TableListAdapter(this);
        kbsxListview.setAdapter(adapter);

        //设置titleView返回监听
        titleView.setOnBackClickListener(new TitleView.OnBackClikListener() {
            @Override
            public void onTitleViewClik(View v) {
                finish();
            }
        });


    }


    /**
     * listview的适配器
     */
    static class TableListAdapter extends BaseAdapter {

        private List<ProjectDataBean.ApproveProjectBean> data = new ArrayList<>();
        private Context context;
        private LayoutInflater inflater;

        public static final int HAS_CHILD = 1;
        public static final int NO_CHILD = 0;


        public TableListAdapter(Context context) {
            this.context = context;
            this.inflater = LayoutInflater.from(context);
        }

        public void setData(List<ProjectDataBean.ApproveProjectBean> data) {
            this.data.clear();
            this.data.addAll(data);
            notifyDataSetChanged();
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        @Override
        public int getItemViewType(int position) {
            ProjectDataBean.ApproveProjectBean approveProjectBean = data.get(position);
            int isChild = approveProjectBean.getIschild();
            return isChild;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            int flag = getItemViewType(position);
            final ProjectDataBean.ApproveProjectBean approveProjectBean = data.get(position);
            String projectName = approveProjectBean.getAppprojectname();
            NoChildViewHolder viewHolder;
            HasChildViewHolder hasChildViewHolder;
            switch (flag) {
                case NO_CHILD:
                    if (convertView == null) {
                        convertView = inflater.inflate(R.layout.kbsx_table_item_layout, null);
                        viewHolder = new NoChildViewHolder(convertView);
                        convertView.setTag(viewHolder);
                    } else {
                        viewHolder = (NoChildViewHolder) convertView.getTag();
                    }
                    viewHolder.nameTextview.setText(projectName);
                    viewHolder.serialNumberTextview.setText(String.valueOf(position + 1));
                    viewHolder.lookTextview.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, BsznActivity.class);
                            intent.putExtra(Constants.SERVER_FLAG, Constants.SERVER_GUIDE);
                            intent.putExtra(Constants.APPROVE_PROJECT_BEAN, approveProjectBean);
                            context.startActivity(intent);
                        }
                    });
                    viewHolder.processTextview.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, FlowActivity.class);
                            intent.putExtra(Constants.APPROVE_PROJECT_BEAN, approveProjectBean);
                            context.startActivity(intent);
                        }
                    });
                    break;
                case HAS_CHILD:
                    if (convertView == null) {
                        convertView = inflater.inflate(R.layout.kbsx_table_parent_layout, null);
                        hasChildViewHolder = new HasChildViewHolder(convertView);
                        convertView.setTag(hasChildViewHolder);
                    } else {
                        hasChildViewHolder = (HasChildViewHolder) convertView.getTag();
                    }
                    hasChildViewHolder.serialNumberTextview.setText(String.valueOf(position + 1));
                    hasChildViewHolder.thingNameTextview.setText(projectName);
                    hasChildViewHolder.childImageview.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, KbsxActivity.class);
                            intent.putExtra(Constants.APPROVE_PROJECT_BEAN, approveProjectBean);
                            context.startActivity(intent);
                        }
                    });
                    break;
            }


            return convertView;
        }

        static class NoChildViewHolder {
            @Bind(R.id.serial_number_textview)
            TextView serialNumberTextview;
            @Bind(R.id.name_textview)
            TextView nameTextview;
            @Bind(R.id.look_textview)
            TextView lookTextview;
            @Bind(R.id.process_textview)
            TextView processTextview;

            NoChildViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }

        static class HasChildViewHolder {
            @Bind(R.id.serial_number_textview)
            TextView serialNumberTextview;
            @Bind(R.id.thing_name_textview)
            TextView thingNameTextview;
            @Bind(R.id.child_imageview)
            ImageView childImageview;

            HasChildViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
